package monster.com.cvh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import monster.com.cvh.R;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:自定义ProgressView
 */
public class RoundProgressBarWithProgress extends HorizontalProgressBarwithProgress {
    private int radius = dp2px(30);//半径
    private int maxPaintWidth;//画笔的宽度

    public RoundProgressBarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //mReachHeight = (int) (mReachHeight * 2.5f);
        obtainStyleAttrs(context, attrs);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    private void obtainStyleAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBarWithProgress);
        radius = (int) ta.getDimension(R.styleable.RoundProgressBarWithProgress_raduis, radius);
        mReachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_reach_height, (int) (mReachHeight * 2.5f));
        mUnreachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressBarwithProgress_progress_unreach_height, (int) (mReachHeight * 2.5f));
        ta.recycle();
    }

    public RoundProgressBarWithProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBarWithProgress(Context context) {
        this(context, null);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //默认四个padding一致
        int expect = radius * 2 + maxPaintWidth + getPaddingLeft() + getPaddingRight();//准确的值
        //根据不同模式指定对应的值
        int width = resolveSize(expect, widthMeasureSpec);
        int height = resolveSize(expect, heightMeasureSpec);
        //取圆的宽高最小的值为相对直径
        int realWidth = Math.min(width, height);
        //取准确半径
        radius = (realWidth - getPaddingLeft() - getPaddingRight() - maxPaintWidth) / 2;
        //确定宽高
        setMeasuredDimension(realWidth, realWidth);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text = getProgress() + "%";
        float textWidth = mPaint.measureText(text);//测量text的宽
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;//计算text的高

        canvas.save();
        canvas.translate(getPaddingLeft() + maxPaintWidth / 2, getPaddingTop() + maxPaintWidth / 2);
        mPaint.setStyle(Paint.Style.STROKE);
        //绘制unReachBar
        mPaint.setColor(mUnreachColor);
        mPaint.setStrokeWidth(mUnreachHeight);
        canvas.drawCircle(radius, radius, radius, mPaint);
        //绘制reachBar
        mPaint.setColor(mReachColor);
        mPaint.setStrokeWidth(mReachHeight);
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;//对应百分比所占的度数
       canvas.rotate(-90,radius,radius);
        canvas.drawArc(new RectF(0, 0, radius * 2, radius * 2), 0, sweepAngle, false, mPaint);
        //绘制文本
       /* mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.rotate(90,radius,radius);
        canvas.drawText(text, radius - textWidth / 2, radius - textHeight, mPaint);*/

        canvas.restore();
    }
}
